package com.muyi.biling.ui;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EActivity;

import java.util.ArrayList;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.muyi.biling.MyApp;
import com.muyi.biling.R;
import com.muyi.biling.bean.AppInfo;
import com.muyi.biling.bean.TabEntity;
import com.muyi.biling.di.DaggerScope;
import com.muyi.biling.di.Injector;
import com.muyi.biling.fragment.GeneralWebFragment;
import com.muyi.biling.fragment.GeneralWebFragment_;
import com.muyi.biling.rest.ApiHelper;

import rx.Subscriber;

/**
 * Created by developer on 18-3-5.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(MainActivity.class)
@EActivity
public class MainActivity extends FragmentActivity {

    @Inject
    ApiHelper mApiHelper;
    private AppInfo mAppInfo;
    private ArrayList<Fragment> mFragmentList;
    private ArrayList<View> mIndicatorList;
    private String mStrBackground;
    private boolean mPadDevice;

    CommonTabLayout mTabLayout;

    @AfterInject
    void initDI() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.initMainActivityComponent().inject(this);
        mPadDevice = ((MyApp) getApplication()).mPadDevice;
        mFragmentList = new ArrayList<>();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mApiHelper.getAppInfo(new GetAppInfoSubscriber());
    }

    void initPadView() {
        setContentView(R.layout.activity_main_tablet);
        //  设置屏幕为横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        ImageView ivLogo = findViewById(R.id.main_tablet2_iv_logo);
        LinearLayout layoutGroup = findViewById(R.id.main_tablet2_layout_group);
        ImageView ivLastIcon = findViewById(R.id.main_tablet2_iv_last_tab);
        TextView tvLastTitle = findViewById(R.id.main_tablet2_tv_last_tab);
        LinearLayout layoutLastTab = findViewById(R.id.main_tablet2_layout_last_tab);
        View viewIndicator = findViewById(R.id.main_tablet2_indicator);

        String borderColor = "";
        mIndicatorList = new ArrayList<>();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        ArrayList<AppInfo.StyleValueBean> images = mAppInfo.Style.Images;
        ArrayList<AppInfo.StyleValueBean> colors = mAppInfo.Style.Colors;

        for (AppInfo.StyleValueBean bean : images) {
            if ("TabbarLogo".equals(bean.Code)) {
                if (!TextUtils.isEmpty(bean.Code)) {
                    Glide
                            .with(this)
                            .load(bean.Value)
                            .into(ivLogo);
                }
            }else if ("TabbarBackground".equals(bean.Code)) {
                mStrBackground = bean.Value;
            }
        }

        for (AppInfo.StyleValueBean bean : colors) {
            if ("TabbarBorderSelected".equals(bean.Code)) {
                borderColor = bean.Value;
            }
        }

        ArrayList<AppInfo.TabBarPageBean> tabBarPages = mAppInfo.Page.TabBarPages;
        for (int i = 0; i < tabBarPages.size(); i ++) {
            AppInfo.TabBarPageBean tabBarPageBean = tabBarPages.get(i);
            GeneralWebFragment fragment = GeneralWebFragment_.builder().url(tabBarPageBean.Url).build();
//            CommonSelectFragment fragment = CommonSelectFragment_.builder().title(tabBarPageBean.Title).build();
            mFragmentList.add(fragment);
            transaction.add(R.id.main_tablet2_layout_content, fragment, String.valueOf(i));
            if (i == 0) {
                transaction.show(mFragmentList.get(i));
            }else {
                transaction.hide(mFragmentList.get(i));
            }

            if (i != tabBarPages.size() - 1) {
                View view = getLayoutInflater().inflate(R.layout.itme_main_tab, null);
                final View indicator = view.findViewById(R.id.item_main_tab_indicator);

                if (!TextUtils.isEmpty(borderColor)) {
                    indicator.setBackgroundColor(Color.parseColor(borderColor));
                }

                if (i == 0) {
                    indicator.setVisibility(View.VISIBLE);
                }else {
                    indicator.setVisibility(View.INVISIBLE);
                }
                mIndicatorList.add(indicator);

                ImageView ivIcon = view.findViewById(R.id.item_main_tab_iv_icon);
                if (!TextUtils.isEmpty(tabBarPageBean.Icon)) {
                    Glide
                            .with(this)
                            .load(tabBarPageBean.Icon)
                            .into(ivIcon);
                }

                TextView tvTitle = view.findViewById(R.id.item_main_tab_tv_title);
                tvTitle.setText(tabBarPageBean.Title);

                view.findViewById(R.id.item_main_tab_layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < mIndicatorList.size(); i ++) {
                            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                            if (i == mIndicatorList.indexOf(indicator)) {
                                mIndicatorList.get(i).setVisibility(View.VISIBLE);
                                transaction.show(mFragmentList.get(i));
                            }else {
                                mIndicatorList.get(i).setVisibility(View.INVISIBLE);
                                transaction.hide(mFragmentList.get(i));
                            }

                            transaction.commit();

                        }
                    }
                });

                layoutGroup.addView(view);

            }else {
                if (!TextUtils.isEmpty(tabBarPageBean.Icon)) {
                    Glide
                            .with(this)
                            .load(tabBarPageBean.Icon)
                            .into(ivLastIcon);
                }

                tvLastTitle.setText(tabBarPageBean.Title);
                if (!TextUtils.isEmpty(borderColor)) {
                    viewIndicator.setBackgroundColor(Color.parseColor(borderColor));
                }
                mIndicatorList.add(viewIndicator);
                layoutLastTab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < mIndicatorList.size(); i ++) {
                            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                            if (i == mIndicatorList.size() - 1) {
                                mIndicatorList.get(i).setVisibility(View.VISIBLE);
                                transaction.show(mFragmentList.get(i));
                            } else {
                                mIndicatorList.get(i).setVisibility(View.INVISIBLE);
                                transaction.hide(mFragmentList.get(i));
                            }

                            transaction.commit();
                        }
                    }
                });

            }

        }

        transaction.commit();

    }

    void initPhoneView() {
        setContentView(R.layout.activity_main_phone);
        //  设置屏幕为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mTabLayout = findViewById(R.id.main_phone_layout_tab);

        ArrayList<AppInfo.TabBarPageBean> tabBarPages = mAppInfo.Page.TabBarPages;
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        ArrayList<Fragment> fragmentList = new ArrayList<>();

        for (AppInfo.TabBarPageBean tabBar : tabBarPages) {
            entities.add(new TabEntity(tabBar.Title));
            fragmentList.add(GeneralWebFragment_.builder().url(tabBar.Url).build());
        }

        ArrayList<AppInfo.StyleValueBean> colors = mAppInfo.Style.Colors;

        mTabLayout.setTabData(entities, this, R.id.main_phone_layout_content, fragmentList);
    }

    private class GetAppInfoSubscriber extends Subscriber<AppInfo> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(AppInfo appInfo) {
            if (appInfo != null && !TextUtils.isEmpty(appInfo.UserStatus)) {
                mAppInfo = appInfo;
                if (mPadDevice) {
                    initPadView();
                }else {
                    initPhoneView();
                }
            }
        }
    }


}
